<template>
  <!-- 重置密码dialog -->
  <el-dialog title="修改库存" :visible.sync="visible" width="30%">
    <el-form
      :model="changeForm"
      label-width="90px"
      :rules="rules"
      ref="changeForm"
    >
      <el-form-item label="库存" prop="stock">
        <el-input
          placeholder="请输入"
          type="number"
          v-model="changeForm.stock"
          style="width: 200px"
        >
        </el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="handelCancel">取 消</el-button>
      <el-button type="primary" @click="handleSubmit">确 定</el-button>
    </span>
  </el-dialog>
</template>
<script>
import { updStock } from "@/api/service/inventory";

export default {
  data() {
    return {
      initObj: {},
      visible: false,
      changeForm: {
        stock: "",
      },
      rules: {
        stock: [
          {
            required: true,
            message: "请输入",
            trigger: "change",
          },
        ],
      },
    };
  },
  props: {},
  methods: {
    // 初始化函数--->父级调用
    handelInit(val) {
      console.log(val);
      this.initObj = val;
      this.visible = true;
    },
    handelCancel() {
      this.$refs.changeForm.resetFields();
      this.visible = false;
    },
    handleSubmit() {
      this.$refs.changeForm.validate((valid) => {
        if (valid) {
          this.handleValidateSuccess();
        } else {
          return false;
        }
      });
    },
    handleValidateSuccess() {
      let obj = {
        id: this.initObj.id,
        stock: this.changeForm.stock,
      };
      updStock(obj).then((res) => {
        console.log(res);
        let { code, msg } = res;
        if (code == "200") {
          this.$message.success("库存修改成功");
          this.$refs.changeForm.resetFields();
          this.visible = false;
          this.$emit("updataList")
        } else {
          this.$message.error(msg);
        }
      });
    },
  },
};
</script>